package com.example.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.client.hospital.HospitalFeignClient;
import com.example.common.util.constant.Constants;
import com.example.common.util.constant.OrderConstants;
import com.example.common.util.enums.BusinessEnum;
import com.example.common.util.enums.PaymentEnums;
import com.example.common.util.enums.PaymentTypeEnums;
import com.example.common.util.exception.BusinessException;
import com.example.common.util.utils.DateUtils;
import com.example.common.util.utils.StringUtils;
import com.example.model.domain.entity.order.OrderInfo;
import com.example.model.domain.entity.order.PaymentInfo;
import com.example.model.vo.order.SignInfoVo;
import com.example.order.mapper.PaymentMapper;
import com.example.order.service.OrderInfoService;
import com.example.order.service.PaymentInfoService;
import com.example.order.service.RefundInfoService;
import com.example.service.util.HttpRequestHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author juzipi
 * @Date 2021/7/26 8:34
 * @Info
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private HospitalFeignClient hospitalFeignClient;



    @Override
    public PaymentInfo getPaymentInfoByOrderIdAndPaymentType(Long orderId, Integer paymentType) {
        //支付记录信息
        PaymentInfo paymentInfo = paymentMapper.selectPaymentInfoByOrderIdAndPaymentType(orderId, paymentType);
        return paymentInfo;
    }



    @Override
    public Integer insertPaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //先判断是否有相同的订单
        PaymentInfo paymentInfo = paymentMapper.selectPaymentInfoByOrderIdAndPaymentType(orderInfo.getId(), paymentType);
        if (StringUtils.isNotNull(paymentInfo)){
            //非空就是已存在
            return 1;
        }
        //添加记录
        paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(DateUtils.getNowDate());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(OrderConstants.UNPAID);//支付中
        String dateToYMD = DateUtils.formatDateToYMD(orderInfo.getReserveDate());
        String subject = dateToYMD+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        return paymentMapper.insertPaymentInfo(paymentInfo);
    }


    @Override
    public Integer updatePaymentType(String outTradeNo, Integer paymentType, Map<String, String> resultMap) {
        PaymentInfo paymentInfo = paymentMapper.selectPaymentInfoByOutTradeNoAndPaymentType(outTradeNo, paymentType);
        paymentInfo.setPaymentStatus(PaymentTypeEnums.PAID.getStatus());
        paymentInfo.setTradeNo(resultMap.get(OrderConstants.TRANSACTION_ID));
        paymentInfo.setCallbackTime(DateUtils.getNowDate());
        paymentInfo.setCallbackContent(resultMap.toString());
        paymentMapper.updatePaymentInfo(paymentInfo);
        //获取到订单信息
        OrderInfo orderInfo = orderInfoService.getOrderInfoById(paymentInfo.getOrderId());
        //更新
        orderInfo.setOrderStatus(OrderConstants.PAID);
        int i = orderInfoService.updateOrderInfo(orderInfo);
        //调用医院接口，更新订单支付信息
        SignInfoVo signInfoVo = (SignInfoVo) hospitalFeignClient.getHospitalSignByHoscode(orderInfo.getHoscode()).get(Constants.DATA);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(OrderConstants.HOSCODE,orderInfo.getHoscode());
        hashMap.put(OrderConstants.HOS_RECORD_ID,orderInfo.getHosRecordId());
        hashMap.put(OrderConstants.TIMESTAMP, HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(hashMap, signInfoVo.getSignKey());
        hashMap.put(OrderConstants.SIGN,sign);
        JSONObject result = HttpRequestHelper.sendRequest(hashMap, signInfoVo.getApiUrl() + "/order/updatePayStatus");
        if (result.getInteger(Constants.CODE) != 200){
            throw new BusinessException(BusinessEnum.OPTION_ERROR);
        }
        return i;
    }

}
